package com.l.d.app.library.util;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.l.d.app.library.view.stiky_header.StickyRecyclerHeadersAdapter;
import com.l.d.app.library.view.stiky_header.StickyRecyclerHeadersDecoration;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.bezierlayout.BezierLayout;

/**
 * Created by linde on 2017/04/20.
 * <p/>
 */
public class ListUtil {
    public static void initRecyclerView(RecyclerView rv, RecyclerView.LayoutManager manager, RecyclerView.Adapter adapter) {
        if (rv == null) {
            throw new NullPointerException("RecyclerView 不能为空");
        }
        if (manager == null) {
            manager = new LinearLayoutManager(rv.getContext());
        }
        rv.setLayoutManager(manager);
        rv.setAdapter(adapter);
        if (adapter instanceof StickyRecyclerHeadersAdapter) {
            StickyRecyclerHeadersDecoration decoration = new StickyRecyclerHeadersDecoration((StickyRecyclerHeadersAdapter) adapter);
            rv.addItemDecoration(decoration);
        }
    }

    public static <L extends RefreshListenerAdapter> void initTwinklingRefresh(TwinklingRefreshLayout refreshLayout, L listener) {
        if (refreshLayout == null) return;
        final Context context = refreshLayout.getContext();
        final BezierLayout header = new BezierLayout(context);
//        final SinaRefreshView header = new SinaRefreshView(context);

        refreshLayout.setHeaderView(header);
        // 允许越界回弹
        refreshLayout.setEnableOverScroll(true);
        // 不允许在越界的时候显示刷新控件
        refreshLayout.setOverScrollTopShow(false);
        refreshLayout.setOverScrollBottomShow(false);
        // 允许下拉刷新
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadmore(false);
        // 刷新监听
        refreshLayout.setOnRefreshListener(listener);
    }
}
